﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;

namespace ServBus.ListForm2010.Utility
{
    public class ListInfo
    {
        public string Name;
        private string _CTypeName;
        private string _listId;
        private string _CTypeId;
        private List<FieldInfo> mFields;
        private List<ListInfo> mLookupLists;

        public string CTypeName
        {
            get { return _CTypeName; }
            set { _CTypeName = value; }
        }

        public string ListId
        {
            get { return _listId; }
            set { _listId = value; }
        }

        public string CTypeId
        {
            get { return _CTypeId; }
            set { _CTypeId = value; }
        }

        public List<ListInfo> LookupLists
        {
            get
            {
                if (mLookupLists == null)
                {
                    mLookupLists = new List<ListInfo>();
                }
                return mLookupLists;

            }
        }
        public List<FieldInfo> Fields
        {
            get
            {
                if (mFields == null)
                {
                    mFields = new List<FieldInfo>();
                }

                return mFields;
            }
            set
            {
                mFields = value;
            }
        }
    }

    public enum FieldType
    {
        TextField,
        DateTime,
        LookUp,
        Combox,
        Counter,
        Boolean,
        Calculated,
        Number,
        Currency,
        UserType,
        MultiText,
        Dropdown,
        RadioButtons,
        CheckBox,
        Url,
        Picture
    }

    public class FieldInfo
    {
        public FieldInfo()
        {

        }

        public FieldInfo CreateFieldInfo(SPField fld, SPList keyList, SPContentType cType)
        {
            Type = FieldType.TextField;
            Name = fld.InternalName;
            DisplayName = fld.Title;
            Required = fld.Required;
            ReadOnly = fld.ReadOnlyField;
            Hidden = fld.Hidden;
            DefaultValue = fld.DefaultValue;
            UniqueName = keyList.Title + cType.Name;

            switch (fld.Type)
            {
                case SPFieldType.DateTime:
                    Type = FieldType.DateTime;
                    SPFieldDateTime dateTimeField = (SPFieldDateTime)fld;
                    DateTimeFieldInfo dateTimeFieldInfo = new DateTimeFieldInfo(this);
                    if (dateTimeField.DisplayFormat.Equals(SPDateTimeFieldFormatType.DateOnly))
                    {
                        dateTimeFieldInfo.DateOnly = true;
                    }
                    else
                    {
                        dateTimeFieldInfo.DateOnly = false;
                    }
                    return dateTimeFieldInfo;
                    break;

                case SPFieldType.URL:
                    SPFieldUrl urlField = (SPFieldUrl)fld;
                    if (urlField.DisplayFormat.Equals(SPUrlFieldFormatType.Hyperlink))
                    {
                        Type = FieldType.Url;
                    }
                    else
                    {
                        Type = FieldType.Picture;
                    }
                    break;

                case SPFieldType.Counter:
                    Type = FieldType.Counter;
                    break;

                case SPFieldType.Boolean:
                    Type = FieldType.Boolean;
                    break;

                case SPFieldType.Calculated:
                    Type = FieldType.Calculated;
                    break;

                case SPFieldType.Lookup:
                    Type = FieldType.LookUp;
                    break;

                case SPFieldType.Integer:
                    Type = FieldType.Number;
                    break;

                case SPFieldType.Number:
                    Type = FieldType.Number;
                    SPFieldNumber numberField = fld as SPFieldNumber;
                    NumberFieldInfo numberFieldInfo = new NumberFieldInfo(this);
                    numberFieldInfo.MinValue = numberField.MinimumValue;
                    numberFieldInfo.MaxValue = numberField.MaximumValue;
                    return numberFieldInfo;
                    break;

                case SPFieldType.Currency:
                    Type = FieldType.Currency;
                    SPFieldCurrency currencyField = fld as SPFieldCurrency;
                    CurrencyFieldInfo currencyFieldInfo = new CurrencyFieldInfo(this);
                    currencyFieldInfo.MinValue = currencyField.MinimumValue;
                    currencyFieldInfo.MaxValue = currencyField.MaximumValue;
                    return currencyFieldInfo;
                    break;

                case SPFieldType.User:
                    Type = FieldType.UserType;
                    break;

                case SPFieldType.Note:
                    Type = FieldType.MultiText;
                    break;

                case SPFieldType.Choice:
                    SPFieldChoice choice = (SPFieldChoice)fld;
                    if ("Dropdown".Equals(choice.EditFormat.ToString()))
                    {
                        Type = FieldType.Dropdown;
                    }
                    else
                    {
                        Type = FieldType.RadioButtons;
                    }
                    break;

                case SPFieldType.MultiChoice:
                    Type = FieldType.CheckBox;
                    SPFieldMultiChoice multiChoice = (SPFieldMultiChoice)fld;
                    break;

                default:
                    Type = FieldType.TextField;
                    break;
            }

            if (Type == FieldType.LookUp)
            {
                SPFieldLookup lkpFld = (SPFieldLookup)fld;
                LookupFieldInfo lkpFldInfo = new LookupFieldInfo(this);
                lkpFldInfo.LookupSiteUrl = keyList.ParentWeb.Url + "/";
                lkpFldInfo.LookupListId = lkpFld.LookupList;
                SPList lookupList = keyList.ParentWeb.Lists[new Guid(lkpFldInfo.LookupListId)];
                lkpFldInfo.LookupListName = lookupList.Title;
                lkpFldInfo.LookupListRelativeUrl = lookupList.RootFolder.Url;
                lkpFldInfo.LookupField = lkpFld.LookupField;
                lkpFldInfo.UniqueName = keyList.Title + cType.Name;
                return lkpFldInfo;
            }
            else if (Type == FieldType.Dropdown || Type == FieldType.RadioButtons)
            {
                SPFieldChoice choice = (SPFieldChoice)fld;
                DropdownFieldInfo dropdownField = new DropdownFieldInfo(this);
                dropdownField.choicesList = SPUtility.GetStringListFromChoiceField(choice);
                dropdownField.DefaultSeletedValue = choice.DefaultValue;
                dropdownField.DefaultValue = choice.DefaultValue;
                dropdownField.UniqueName = keyList.Title + cType.Name;
                return dropdownField;
            }
            else if (Type == FieldType.CheckBox)
            {
                SPFieldMultiChoice choiceField = (SPFieldMultiChoice)fld;
                DropdownFieldInfo dropdownField = new DropdownFieldInfo(this);
                dropdownField.choicesList = SPUtility.GetStringListFromChoiceField(choiceField);
                dropdownField.DefaultSeletedValue = choiceField.DefaultValue;
                dropdownField.DefaultValue = choiceField.DefaultValue;
                dropdownField.UniqueName = keyList.Title + cType.Name;
                return dropdownField;
            }

            return this;
        }

        public string Name { get; set; }
        public string DisplayName { set; get; }
        public bool Required { get; set; }
        public FieldType Type { get; set; }
        public bool ReadOnly { get; set; }
        public bool Hidden { get; set; }
        public string DefaultValue { get; set; }
        public string UniqueName { get; set; }
    }

    public class NumberFieldInfo : FieldInfo
    {
        public NumberFieldInfo(FieldInfo fieldInfo)
        {
            this.Name = fieldInfo.Name;
            this.DisplayName = fieldInfo.DisplayName;
            this.Required = fieldInfo.Required;
            this.ReadOnly = fieldInfo.ReadOnly;
            this.Type = fieldInfo.Type;
            this.DefaultValue = fieldInfo.DefaultValue;
            this.Hidden = fieldInfo.Hidden;
        }

        public double MinValue { get; set; }

        public double MaxValue { get; set; }
    }

    public class CurrencyFieldInfo : FieldInfo
    {
        public CurrencyFieldInfo(FieldInfo fieldInfo)
        {
            this.Name = fieldInfo.Name;
            this.DisplayName = fieldInfo.DisplayName;
            this.Required = fieldInfo.Required;
            this.ReadOnly = fieldInfo.ReadOnly;
            this.Type = fieldInfo.Type;
            this.DefaultValue = fieldInfo.DefaultValue;
            this.Hidden = fieldInfo.Hidden;
        }

        public double MinValue { get; set; }

        public double MaxValue { get; set; }
    }

    public class DateTimeFieldInfo : FieldInfo
    {
        public DateTimeFieldInfo(FieldInfo fieldInfo)
        {
            this.Name = fieldInfo.Name;
            this.DisplayName = fieldInfo.DisplayName;
            this.Required = fieldInfo.Required;
            this.ReadOnly = fieldInfo.ReadOnly;
            this.Type = fieldInfo.Type;
            this.DefaultValue = fieldInfo.DefaultValue;
            this.Hidden = fieldInfo.Hidden;
        }

        public bool DateOnly { get; set; }
    }

    public class LookupFieldInfo : FieldInfo
    {
        public LookupFieldInfo(FieldInfo fieldInfo)
        {
            this.Name = fieldInfo.Name;
            this.DisplayName = fieldInfo.DisplayName;
            this.Required = fieldInfo.Required;
            this.ReadOnly = fieldInfo.ReadOnly;
            this.Type = fieldInfo.Type;
            this.DefaultValue = fieldInfo.DefaultValue;
            this.Hidden = fieldInfo.Hidden;
        }

        public string LookupField { get; set; }
        public string LookupSiteUrl { get; set; }
        public string LookupListId { get; set; }
        public string LookupListRelativeUrl { get; set; }
        public string LookupListName { get; set; }
        public string LookupFieldType { get; set; }
        public string LookupFieldReadOnly { get; set; }
    }

    public class DropdownFieldInfo : FieldInfo
    {
        public DropdownFieldInfo(FieldInfo fieldInfo)
        {
            this.Name = fieldInfo.Name;
            this.DisplayName = fieldInfo.DisplayName;
            this.Required = fieldInfo.Required;
            this.ReadOnly = fieldInfo.ReadOnly;
            this.Type = fieldInfo.Type;
            this.DefaultSeletedValue = fieldInfo.DefaultValue;
            this.DefaultValue = fieldInfo.DefaultValue;
            this.Hidden = fieldInfo.Hidden;
        }

        public List<string> choicesList;
        public string DefaultSeletedValue;
    }

    public class CheckBoxFieldInfo : DropdownFieldInfo
    {
        public CheckBoxFieldInfo(FieldInfo fieldInfo)
            : base(fieldInfo)
        { }
    }
}
